package com.students.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.students.mapper.LocalstorageMapper;
import com.students.pojo.Localstorage;
import com.students.service.ILocalstorageService;
import com.students.utils.AdminUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author caohanqing
 * @since 2022-07-10
 */
@Service
@RequiredArgsConstructor
public class LocalstorageServiceImpl extends ServiceImpl<LocalstorageMapper, Localstorage> implements ILocalstorageService {

    @Value("${file.windows.path}")
    private String winPath;

    private final LocalstorageMapper localstorageMapper;

    //创建文件夹
    @Override
    public String ImgFound(MultipartFile file) throws IOException {
        String s1 = "图片";
        String s = winPath + s1;
        File file1 = new File(s);
        if (!file1.exists()) file1.mkdirs();
        //处理文件上传
        file.transferTo(new File(file1,file.getOriginalFilename()));
        return s1;
    }
    @Override
    public String TxtFound(MultipartFile file) throws IOException {
        String s1 = "文件";
        String s = winPath + s1;
        File file1 = new File(s);
        if (!file1.exists()) file1.mkdirs();
        //处理文件上传
        file.transferTo(new File(file1,file.getOriginalFilename()));
        return s1;
    }
    @Override
    public String DateFound(MultipartFile file) throws IOException {
        //处理根据日期生成目录
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());//文件夹
        String s1 = winPath + format;
        File dataDtr = new File(s1);
        if (!dataDtr.exists())dataDtr.mkdirs();
        //处理文件上传
        file.transferTo(new File(dataDtr,file.getOriginalFilename()));
        return format;
    }

    //根据用户id和上级目录分段查询文件列表
    @Override
    public List<Localstorage> getFindAllID(int superiorId) {
        QueryWrapper<Localstorage> localstorageQueryWrapper=new QueryWrapper<>();
        localstorageQueryWrapper.eq("superiorId",superiorId);//上级id
        localstorageQueryWrapper.eq("createBy", AdminUtil.getCurrentAdmin().getId());
        List<Localstorage> departmentList=localstorageMapper.selectList(localstorageQueryWrapper);
        return departmentList;
    }

}
